/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-12 下午6:48 Created by dwzhang
 */
package com.jade.bss.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jade.framework.base.vfile.VFileInfo;
import com.jade.framework.base.vfile.VFileManager;
import com.jade.framework.base.vfile.VFileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a>
 * @version 1.0 15-1-12 下午6:48
 */
@RestController
@RequestMapping (value = "/vfs")
@Api(value = "文件处理", hidden = true)
public class VFileController
{
    @RequestMapping (value = "/{dataType}/**")
    @ApiOperation(value = "下载", hidden = true)
    public void downloadFile(@PathVariable (value = "dataType") String dataType,  HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        VFileManager fileManager = VFileUtils.getFileManager(dataType);
        String requestUri = request.getRequestURI();
        int index = requestUri.indexOf(dataType);
        String fileId = requestUri.substring(dataType.length() + index);
        VFileInfo info = fileManager.getFileInfo(dataType, fileId);
        if (info != null) {
            response.setContentType(info.getContentType());
            response.setHeader("Content-Length", String.valueOf(info.getSize()));;
            fileManager.downloadFile(info, response.getOutputStream());
        }
        else {
            //return 404.
            response.setStatus(404);
        }
    }
}
